package com.xl.studentpass.controller;

import com.xl.studentpass.entity.po.OrderOverViewPo;
import com.xl.studentpass.entity.vo.OrderVo;
import com.xl.studentpass.service.AcceptTaskService;
import com.xl.studentpass.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author: xl
 * @createTime: 2023/5/28 21:44:47
 * @program: student-pass
 * @e-mail: 2199396150@qq.com
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/accept")
@Api(tags = "用户接单相关接口")
public class OrderController {

    @Resource
    AcceptTaskService acceptTaskService;

    @PostMapping("/create")
    @ApiOperation("用户接单")
    public R create(@RequestBody Map<String, Integer> orderInfo) {
        log.info(this.getClass().getSimpleName() + "=================用户接单");
        try {
            Integer id = acceptTaskService.create(orderInfo);
            if (ObjectUtils.isEmpty(id) || id.equals(-1)) {
                return R.error("接单失败");
            }
            return R.ok().put("orderId", id);
        } catch (RuntimeException e) {
            log.error("接单失败");
            return R.error("接单失败");
        }
    }

    @PostMapping("/cancel")
    @ApiOperation("取消订单")
    public R cancel(@RequestBody OrderVo orderVo) {
        log.info(this.getClass().getSimpleName() + "=======================取消订单");
        Integer id = acceptTaskService.cancel(orderVo);
        if (ObjectUtils.isEmpty(id) || id.equals(-1)) {
            return R.error("取消订单失败");
        }
        return R.ok().put("orderId", id);
    }

    @PostMapping("/complete")
    @ApiOperation("完成订单")
    public R complete(@RequestBody OrderVo orderVo) {
        log.info(this.getClass().getSimpleName() + "=====================完成订单");
        Integer id = acceptTaskService.complete(orderVo);
        if (ObjectUtils.isEmpty(id) || id.equals(-1)) {
            return R.error("订单完成失败");
        }
        return R.ok().put("orderId", id);
    }

    @PostMapping("/delete")
    @ApiOperation("删除订单")
    public R delete(@RequestBody OrderVo orderVo) {
        log.info(this.getClass().getSimpleName() + "============================删除订单");
        Integer id = acceptTaskService.delete(orderVo);
        if (ObjectUtils.isEmpty(id) || id.equals(-1)) {
            return R.error("删除失败");
        } else {
            return R.ok().put("orderId", orderVo.getOrderId());
        }
    }

    @PostMapping("/orderInfo")
    @ApiOperation("查看订单")
    public R viewOrderDetails(@RequestBody OrderVo orderVo) {
        log.info(this.getClass().getSimpleName() + "================================查看订单");
        OrderOverViewPo orderView = acceptTaskService.viewOrderDetails(orderVo);
        if (ObjectUtils.isEmpty(orderView)) {
            return R.error("获取订单详情数据失败");
        }
        return R.ok().put("orderInfo", orderView);
    }
}